import { useAuthStore } from '@/store/auth';
import { useChain } from '@/context/ChainContext';
import { useMemo } from 'react';
import { useVestingCheck } from './useVestingCheck';

// Define user roles and permissions
export enum UserRole {
  SUPER_ADMIN = 'super_admin',
  VESTING_USER = 'vesting_user',
  GUEST = 'guest'
}

export enum Permission {
  // Admin permissions
  VIEW_DASHBOARD = 'view_dashboard',
  MANAGE_USERS = 'manage_users', 
  MANAGE_MINERS = 'manage_miners',
  MANAGE_REFERRALS = 'manage_referrals',
  MANAGE_DTVC = 'manage_dtvc',
  MANAGE_ANNOUNCEMENTS = 'manage_announcements',
  MANAGE_VESTING = 'manage_vesting',
  VIEW_BLOCKCHAIN = 'view_blockchain',
  MANAGE_SETTINGS = 'manage_settings',
  
  // Vesting permissions
  VIEW_MY_VESTING = 'view_my_vesting',
  CLAIM_VESTING = 'claim_vesting',
}

// Define permissions for each role
const ROLE_PERMISSIONS: Record<UserRole, Permission[]> = {
  [UserRole.SUPER_ADMIN]: [
    Permission.VIEW_DASHBOARD,
    Permission.MANAGE_USERS,
    Permission.MANAGE_MINERS,
    Permission.MANAGE_REFERRALS,
    Permission.MANAGE_DTVC,
    Permission.MANAGE_ANNOUNCEMENTS,
    Permission.MANAGE_VESTING,
    Permission.VIEW_BLOCKCHAIN,
    Permission.MANAGE_SETTINGS,
    Permission.VIEW_MY_VESTING,
    Permission.CLAIM_VESTING,
  ],
  [UserRole.VESTING_USER]: [
    Permission.VIEW_MY_VESTING,
    Permission.CLAIM_VESTING,
  ],
  [UserRole.GUEST]: [],
};

// Super admin wallet addresses (all lowercase for comparison)
const SUPER_ADMIN_WALLETS = [
  '0x44fafd20c06299dadf4db652d6aba64eebb0bdd6', // Original admin account
  '0x4e426facdf1ca16df31f8dbaadee4fe5d2f79d50', // Developer account
  '0xeecb29db676e3a0b83b34510e45f5dd82b37ae96', // Additional admin account
  '0x58f4f5a8c947cbbd53ec6e159326a7bed29a052f', // Additional admin account
  '0x83039ac61346ff07bc83f2ee3661bb258f6cfdb2', // New super admin account
];

// Helper function to determine user role
export function getUserRole(walletAddress?: string, isVestingBeneficiary?: boolean): UserRole {
  if (!walletAddress) return UserRole.GUEST;
  
  // Check if super admin (compare lowercase addresses)
  if (SUPER_ADMIN_WALLETS.includes(walletAddress.toLowerCase())) {
    return UserRole.SUPER_ADMIN;
  }
  
  // Check if user is a vesting beneficiary
  if (isVestingBeneficiary) {
    return UserRole.VESTING_USER;
  }
  
  return UserRole.GUEST;
}

export function usePermissions() {
  const { currentChain } = useChain();
  const { getAdminForChain } = useAuthStore();
  const { isVestingBeneficiary } = useVestingCheck();
  
  const admin = getAdminForChain(currentChain);
  
  const userRole = useMemo(() => {
    return getUserRole(admin?.wallet_address, isVestingBeneficiary);
  }, [admin?.wallet_address, isVestingBeneficiary]);
  
  const permissions = useMemo(() => {
    return ROLE_PERMISSIONS[userRole] || [];
  }, [userRole]);
  
  const hasPermission = (permission: Permission): boolean => {
    return permissions.includes(permission);
  };
  
  const hasAnyPermission = (permissionList: Permission[]): boolean => {
    return permissionList.some(permission => permissions.includes(permission));
  };
  
  const hasAllPermissions = (permissionList: Permission[]): boolean => {
    return permissionList.every(permission => permissions.includes(permission));
  };
  
  const isSuperAdmin = userRole === UserRole.SUPER_ADMIN;
  const isVestingUser = userRole === UserRole.VESTING_USER;
  const isGuest = userRole === UserRole.GUEST;
  
  return {
    userRole,
    permissions,
    hasPermission,
    hasAnyPermission,
    hasAllPermissions,
    isSuperAdmin,
    isVestingUser,
    isGuest,
  };
}